<?php
/**
 * Created by PhpStorm.
 * User: lixiaohui
 * Date: 2015/9/22
 * Time: 11:32
 */
/**请求工具方法
 * @param $url 请求url
 * @param $post_string 参数串
 * @return mixed
 */
function post($url, $post_string)
{
    $ch = curl_init($url);
    if (isset($post_string)) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
    $this_header = array(
        "Content-type: application/x-www-form-urlencoded;charset=utf-8"
    );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

//得到uuid
function getUUIDShort()
{
    $queryResult = M()->query('select uuid_short()');
    return $queryResult[0]['uuid_short()'];
}

//得到JsApiPay实例
function getTools()
{
    include_once('WxpayAPI/example/WxPay.JsApiPay.php');
    include_once('WxpayAPI/lib/WxPay.Data.php');
    include_once('WxpayAPI/lib/WxPay.Config.php');
    return new \JsApiPay();
}

//得到openId
function getOpenId()
{
    //return "oNw7mjgP3jvT-g5cjpMyPxwSXNu0";
    if(session('?openId')){
        return session('openId');
    }
    $tools = getTools();
    $openId = $tools->GetOpenid();
    session('openId',$openId);
    return $openId;

}

/**
 * 生成接口参数串
 * @param $cmd 请求的命令
 * @param $argsArray 参数数组
 * @return string 类似cmd=getUserInfo&args={}&signData=AAAAAA 的字符串
 * @throws \Think\Exception
 */
function getPostString($cmd, $argsArray)
{
    if (!is_array($argsArray)) {
        throw new \Think\Exception('argsArray expected to be array');
    }
    $postString = "cmd=$cmd&";
    //参数
    $args = array(
        'reqid' => C('REQID'),
        'opercode' => C('OPERCODE'),
        'timestamp' => sprintf('%s', time()),
        'signdata' => '0D36EF27F906',
        'data' => $argsArray
    );

    $postString .= "args=" . json_encode($args,JSON_UNESCAPED_UNICODE) . '&';
    //签名字符串
    $signString = json_encode($args,JSON_UNESCAPED_UNICODE) . C('SECRET_KEY');
    //签名
    $signData = strtoupper(md5($signString));

    $postString .= 'signdata=' . $signData;

    return $postString;
}

/*function getPostString($cmd, $argsArray)
{
    if (!is_array($argsArray)) {
        throw new \Think\Exception('argsArray expected to be array');
    }
    $postString = "cmd=$cmd&";
    //参数
    $args = array(
        'reqid' => C('REQID'),
        'opercode' => C('OPERCODE'),
        'timestamp' => sprintf('%s', time()),
        'signdata' => C('SECRET_KEY'),
        'data' => $argsArray
    );

    $postString .= "args=" . json_encode($args,JSON_UNESCAPED_UNICODE) . '&';
    //签名字符串
    $signString = json_encode($args,JSON_UNESCAPED_UNICODE) . C('SECRET_KEY');
    //签名
    $postString .= 'signdata=123456';

    return $postString;
}*/

/**
 * @param $queryType 判断类型 1为用户编号，2为身份证
 * @param $queryKey 搜索关键字
 * @throws \Think\Exception 判断用户是否存在 工具方法
 */
function isUserExist($queryType, $queryKey)
{
    $args = array(
        "queryType" => $queryType == C('USER_INFO_QUERY_TYPE_USER_ID') ? C('USER_INFO_QUERY_TYPE_USER_ID') : C('USER_INFO_QUERY_TYPE_IDENTITY'),
        "queryKey" => $queryKey
    );
    $postString = getPostString('GetUserInfo', $args);

    $retStr = post(C('INTERFACE_BASE_URL'),$postString);
    $retObj = json_decode($retStr,true);
    if(count($retObj['data']['list']) == 0){
        return false;
    }
    return true;
}

/**
 * @param $cmd
 * @return mixed
 * @throws \Think\Exception 请求工具方法
 */
function doQuery($cmd, $args)
{
    if (!is_array($args)) {
        throw new Exception('$args 参数应该为数组');
    }

    $postString = getPostString($cmd, $args);
    \Think\Log::write($postString);
    $retStr = post(C('INTERFACE_BASE_URL'), $postString);
    \Think\Log::write($retStr);
    $retObj = json_decode($retStr, true);
    //dump($retObj);
    $retObj['userid'] = $args['userid'];
    $retObj['year'] = $args['queryyear'];
    return $retObj;
}

function getBottomMenu(){
    //取菜单
    $menuTable = M("wx_public_menu");
    //取一级菜单
    $menu = $menuTable -> query("select button from jt_wx_public_menu j where sub_button is null or sub_button='' order by j.order;");
    //取二级菜单
    foreach($menu as $key => $value){
        $menu[$key]['sub_buttons'] = $menuTable -> alias("j") -> field('sub_button,url') -> where("button='%s' and sub_button !='' and publicid='%s'",$value['button'],C('PUBLIC_ID'))
            -> order('j.order,j.button,j.sub_button') -> select();
    }
    //dump($menu);
    return $menu;
}